/**
 * Copyright (c) 2011 Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package wiiWired.controller.console;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import wiiWired.controller.data.Race;
import wiiWired.controller.data.RaceModel;
import wiiWired.controller.mote.ControllerModel;
import wiiWired.controller.mote.MoteController;
import wiiWired.controller.mote.TimerController;

/**
 * Class which handles the table of motes
 * Also handles the list of races
 * 
 * @author Daniel Centore
 * 
 */
public class RaceTableModel extends AbstractTableModel
{

	private static final long serialVersionUID = 1L;

	private RaceModel raceModel = null;

	public RaceTableModel(RaceModel raceModel)
	{
		this.raceModel = raceModel;
	}

	//	private static List<Race> races = new ArrayList<Race>(); //list of past races (oldest 0..1..2...length-1 newest)
	//
	//	/**
	//	 * Registers race r with the list of races
	//	 * @param r
	//	 */
	//	public static synchronized void registerRace(Race r)
	//	{
	//		races.add(r);
	//	}

	/**
	 * returns the name of a column
	 * @param col The index of the column
	 */
	@Override
	public String getColumnName(int col)
	{
		MoteController[] model = ControllerModel.getModel();

		if (col == 0)
			return "Race" + (TimerController.isTiming() ? " (" + formatTime(TimerController.getCurrentTime()) + ")" : "");

		col--; //offset by 1

		boolean nunchuk = !(col % 2 == 0);
		int pos = col / 2;

		if (nunchuk)
			return model[pos].getNunchukName();

		return model[pos].getName();
	}

	/**
	 * Returns the number of columns we have
	 */
	@Override
	public int getColumnCount()
	{
		return (ControllerModel.getModel().length * 2) + 1;
	}

	/**
	 * Returns the number of races we have
	 */
	@Override
	public int getRowCount()
	{
		//		return races.size() + 1; //+1 for currently running
		return raceModel.getSize();
	}

	/**
	 * Returns the contents of a cell (for JTable)
	 */
	@Override
	public Object getValueAt(int row, int col)
	{
		if (col == 0)
			return getRowCount() - row;

		MoteController[] model = ControllerModel.getModel();

		col--; //offset by 1

		boolean nunchuk = !(col % 2 == 0);
		int pos = col / 2;

		String bta = model[pos].getBluetoothAddress();

		row = Math.abs(row - raceModel.getSize()) - 1;

		if (nunchuk)
			return raceModel.getRace(row).getNunchukTimes(bta);
		else
			return raceModel.getRace(row).getTimes(bta);

	}

	/**
	 * Has something to do with multiline stuff
	 * creds: http://www.codeguru.com/java/articles/131.shtml
	 */
	@Override
	public Class<String> getColumnClass(int columnIndex)
	{
		return String.class;
	}

	/**
	 * Formats a time for pretty viewing
	 * 
	 * @param time Time in ms
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String formatTime(long time)
	{
		if (time < 0)
			return "-";

		Date date = new Date(time);
		String s = "";

		String secs = date.getSeconds() + "";
		if (secs.length() == 1)
			secs = "0" + secs;

		String decimal;
		try
		{
			decimal = ((new BigDecimal(time).divide(new BigDecimal(1000))) + "").split("\\.")[1];
		} catch (ArrayIndexOutOfBoundsException e)
		{ //if we get all 0s (which instead of 6.000 is 6)
			decimal = "00";
		}
		while (decimal.length() < 2)
			decimal += "0";
		if (decimal.length() == 3)
			decimal = decimal.substring(0, 2);

		s += date.getMinutes() + ":" + secs + "." + decimal;

		return s;
	}

}
